using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using UnityEngine.SceneManagement;
using KFGame;


namespace KFGame
{
    public class KFBackViewManager : MonoBehaviour
    {
        public static string sceneName = "KF_BackView_Scene";

        /// <summary>
        /// 创建一个管理场景
        /// </summary>
        public static void CreateBackSceen()
        {
            KF kF = KF.Instantiation();
            Scene BScene = kF.sceneManager.CreateScene(sceneName);
            // kF.sceneManager.SetActiveScene(sceneName);

            GameObject tkf = new GameObject("KFBackViewManager");
            KFSceneManager.MoveGameObjectToScene(tkf, BScene);
            KFBackViewManager kvm = tkf.AddComponent<KFBackViewManager>();
            kvm.isMain = true;
            // kvm.parentScene = ac.name;

            GameObject uiCamera = Resources.Load("KFUICamera") as GameObject;
            if (uiCamera)
                GameObject.Instantiate(uiCamera, tkf.transform);

            // DontDestroyOnLoad(tkf);
        }
        public static void destroySceen()
        {
            KF.kf.sceneManager.UnLoadScene(sceneName);
        }

        [HideInInspector]
        public string parentScene;

        //当前组件名
        public string viewname = "KFBackView";

        //主页
        public string mainview = "KFBackView";

        public delegate void ViewEvent(string e, bool isMain);
        public ViewEvent onViewEvent;

        public bool isMain = false;


        //事件管理中心
        public KFEventManager kfeventManger;


        void Start()
        {
            if (isMain)
                loading(mainview, isMain);

            initView();
        }

        public void initView()
        {


            kfeventManger = KF.kf.kfevents;
            kfeventManger.addLisener(OnEvent, KFEventManager.TYPE_KFEVENT_DEFAULT);
            Debug.Log("bakc: add event");
        }

        /// <summary>
        /// 回调处理
        /// </summary>
        /// <param name="e"></param>
        /// <param name="d"></param>
        private void OnViewEvent(string e, bool isMain)
        {
            Debug.Log(" to close " + e + ":" + isMain);

            //退出 后台
            if (e == "close" && isMain)
            {
                popExitManagerEevent();
                return;
            }
            //子层退出
            if (e == "close")
            {
                loading(mainview, true);
                return;
            }


        }

        /// <summary>
        /// 加载组件
        /// </summary>
        /// <param name="name"></param>
        private void loading(string name, bool isMan)
        {
            GameObject g = loadResouce(name);
            if (g == null)
            {
                Debug.LogError("KF BackViewManger  mainviw  no loaded ");
            }
            KFBackViewManager bvm = g.GetComponent<KFBackViewManager>();
            if (bvm)
                bvm.isMain = isMan;


        }


        /// <summary>
        /// 实例化资源
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public GameObject loadResouce(GameObject gb)
        {
            GameObject g = GameObject.Instantiate(gb);

            KFBackViewManager bvm = g.GetComponent<KFBackViewManager>();
            if (bvm)
                bvm.onViewEvent += OnViewEvent;

            Scene BScene = KF.kf.sceneManager.scene(sceneName);
            KFSceneManager.MoveGameObjectToScene(g, BScene);

            return g;
        }
        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public GameObject loadResouce(string name)
        {
            GameObject gb = Resources.Load(name) as GameObject;
            if (gb == null)
            {
                Debug.LogError("KF BackViewManger Resources load error " + name);
                return null;
            }


            GameObject g = GameObject.Instantiate(gb);
            KFBackViewManager bvm = g.GetComponent<KFBackViewManager>();
            if (bvm != null)
            {
                bvm.onViewEvent += OnViewEvent;

            }
            Scene BScene = KF.kf.sceneManager.scene(sceneName);
            KFSceneManager.MoveGameObjectToScene(g, BScene);
            return g;
        }

        /// <summary>
        /// 事件处理接口
        /// </summary>
        /// <param name="e"></param>
        public virtual void OnEvent(KFEvent e)
        {
            Debug.Log(" backView   to OnEvent " + e.name);
            //重复调用打开事件
            if (e.name == KFEEVENTS.KFE_SCENE_BACKVIEW_OPEN)
            {
                popExitManagerEevent();
                return;
            }

            //接到请求关闭
            if (e.name == KFEEVENTS.KFE_SCENE_BACKVIEW_CLOSE)
            {
                //事件调用
                // close();
                Destroy(gameObject);
                return;
            }

        }

        /// <summary>
        /// 抛出退出场景事件
        /// </summary>
        public void popExitManagerEevent()
        {
            KF.kf.kfevents.Pop(new KFEvent(KFEEVENTS.KFE_SCENE_BACKVIEW_CLOSE, KFEventManager.TYPE_KFEVENT_DEFAULT));
        }

        /// <summary>
        /// 消毁前处理
        /// </summary>
        void OnDestroy()
        {
            if (KF.kf != null)
                KF.kf.kfevents.removeListener(OnEvent, KFEventManager.TYPE_KFEVENT_DEFAULT);
        }

        /// <summary>
        /// 关闭组件
        /// </summary>
        public void close()
        {
            if (onViewEvent != null)
                onViewEvent("close", isMain);

            Destroy(gameObject);
        }



    }
}
